package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 云南网解析
 * @author Persh
 *
 */
public class YunNanNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://.*.yunnan.cn/.*/[0-9]{4}-[0-9]{2}/[0-9]{2}/[^.]*.htm",
		"http://.*.yunnan.cn/.*/[0-9]*.html",
		"http://.*.yncunguan.com/.*/[0-9]{4}-[0-9]{2}/[0-9]{2}/[^.]*.htm",
		"http://.*.yunnan.cn/.*/[0-9]{4}/.*/[0-9]*.html"};
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);		
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
			title =doc.select("div#layer213").text();
			if(title==null || title.trim().length()==0){
				title =doc.select("div.news_main>h1").text();
				if(title==null || title.trim().length()==0){
					title =doc.select("div#layer02_41").text();
					if(title==null || title.trim().length()==0){
						title =doc.select("div#layer52").text();
						if(title==null || title.trim().length()==0){
							title =doc.select("div.layer051_last").text();
							if(title==null || title.trim().length()==0){
								title =doc.select("div.layer051").text();
								if(title==null || title.trim().length()==0){
									title =doc.select("div#layer032").text();
									if(title==null || title.trim().length()==0){
										title=doc.select("title").text();
										title=StringUtils.substringBefore(title, "-");
										if(title==null || title.trim().length()==0){
										}
									}
								}
							}
						}
					}
				}
			}
			
			content=HtmlCleaner.getContentHtml(url,doc.select("div#layer216"));
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.news_main>div.news_article"));
				if(content==null || content.trim().length()==0){
					content=HtmlCleaner.getContentHtml(url,doc.select("div#layer02_42"));
					if(content==null || content.trim().length()==0){
						content=HtmlCleaner.getContentHtml(url,doc.select("div#layer53"));
						if(content==null || content.trim().length()==0){
							content=HtmlCleaner.getContentHtml(url,doc.select("div.layer054_last"));
							if(content==null || content.trim().length()==0){
								content=HtmlCleaner.getContentHtml(url,doc.select("div.layer054"));
								if(content==null || content.trim().length()==0){
									content=HtmlCleaner.getContentHtml(url,doc.select("div#layer035"));
									if(content==null || content.trim().length()==0){
										content=HtmlCleaner.getContentHtml(url,doc.select("div.a-body"));
										if(content==null || content.trim().length()==0){
											content=HtmlCleaner.getContentHtml(url,doc.select("div.wmidl1_3"));
											if(content==null || content.trim().length()==0){
												content=HtmlCleaner.getContentHtml(url,doc.select("div#imgContent"));
											}
										}
									}
								}
							}
						}
					}
				}
			}
			content=content.replace("正在加载中... ", "");

			String tempdate=doc.select("div#layer214").text();
			if(tempdate==null || tempdate.trim().length()==0){
				tempdate=doc.select("div.news_main>div.news_info").text();
				if(tempdate==null || tempdate.trim().length()==0){
					tempdate=doc.select("div#layer02_44").text();
					if(tempdate==null || tempdate.trim().length()==0){
						tempdate=doc.select("div#layer54").text();
						if(tempdate==null || tempdate.trim().length()==0){
							tempdate=doc.select("div.layer052_last").text();
							if(tempdate==null || tempdate.trim().length()==0){
								tempdate=doc.select("div.layer052").text();
								if(tempdate==null || tempdate.trim().length()==0){
									tempdate=doc.select("div#layer0332").text();
									if(tempdate==null || tempdate.trim().length()==0){
										tempdate=doc.select("div.a-info").text();
										if(tempdate==null || tempdate.trim().length()==0){
											tempdate=doc.select("P.wpic1").text();
											if(tempdate==null || tempdate.trim().length()==0){
												tempdate=doc.select("div.layer31").text();
												if(tempdate==null || tempdate.trim().length()==0){
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			date=DateUtils.matchDate(tempdate);
			if(date==null){
				date=DateUtils.matchDate(doc.select(".xt2").text());
			}
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);	
		
			String authorStr=doc.select("html body div.middiv1 div.middiv2 div#layer03 div#layer05 div#layer053 div#layer214").text();
			if(StringUtils.isBlank(authorStr)){
				authorStr=doc.select("div.layer31").text();
			}
			if(authorStr!=null && authorStr.indexOf("来源：")!=-1){
				author=StringUtils.substringBetween(authorStr, "来源：","  ");
				if(author==null){
					author=StringUtils.substringAfter(authorStr, "来源：");
					if(author != null){
						author=author.trim();
					}
				}
			}
			news.setAuthor(author);
		return news;
	}
	
	public static void main(String[] args) {
		YunNanNewsAnalyse analyse=new YunNanNewsAnalyse();
		String url="http://3g.yunnan.cn/html/2014-08/14/content_3327268.htm";
		url=UrlArgumentTop.FromatUrl(url);
		System.err.println(url);
		System.err.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
